<?php

namespace app\api\controller;

use think\Db;

//首页
class Index extends BaseController
{
    //首页
    public function index()
    {
        $user_info = session('user_info');

        return success($user_info);
    }

    // 首页顶部菜单的红点
    public function getRedPointNum()
    {
        $user_info = session('user_info');
        //红包
        $query = ['user_id' => $user_info['id'], 'not_open' => 1];
        $data['red_packet'] = Db::name('red_packet')->where($query)->count();

        return success($data);
    }

    // 公告
    public function getNotice()
    {
        //公告的红点是本地localstorage判断
        $user_info = session('user_info');
        $limit = input('post.limit', 3);
        $data = Db::name('notice')->whereOr(function ($query) {
            $query->where(['come_from' => '平台']);
        })->whereOr(function ($query) use ($user_info) {
            $query->where(['come_from' => '帮主', 'gang_id' => $user_info['gang_id']]);
        })->where(['time' => ['lte', time()], 'over_time' => ['gt', time()]])->limit($limit)->order(['is_top' => -1, 'time' => -1])->select();

        return success($data);
    }

    // 筛选条件
    public function getFilterData()
    {
        $brand_data = Db::name('brand')->field('id,name')->select();
        $type_data = Db::name('type')->field('id,name')->select();
        $data = [['name' => '类别', 'data' => $type_data], ['name' => '品牌', 'data' => $brand_data]];

        return success($data);
    }

    //发布公告
    public function publishNotice()
    {
        $user_info = session('user_info');
        $content = input('post.content', '');
        if (!$content) {
            return error('缺少内容');
        }
        $is_top = input('post.is_top', 0);
        if (!$user_info['gang_id']) {
            return error('您未加入帮会');
        }
        if (5 != $user_info['gang_role']) {
            return error('您的权限不够');
        }
        $data = ['title' => $content, 'come_from' => '帮主', 'time' => time(), 'over_time' => strtotime(date('Y-m-d H:i:s').' +1 month -1 day'), 'is_top' => intval($is_top), 'gang_id' => $user_info['gang_id'], 'user_id' => $user_info['id']];
        Db::name('notice')->insert($data);

        return success();
    }
}
